/*
 * Connection.cpp
 *
 *  Created on: 2010-7-13
 *      Author: zhzuo
 */

#include "Connection.h"

bool Connection::OpenDb()
{
    conn = QSqlDatabase::addDatabase("QSQLITE");
    conn.setDatabaseName(DBNAME);
    return conn.open();
}

QSqlQuery* Connection::CreateQueryConn()
{
	QSqlQuery* query=new QSqlQuery(conn);
	return query;
}

bool Connection::IsInited()
{
    QSqlQuery* query=CreateQueryConn();
    query->exec("select * from config where name='createdate'");
    return query->next();
}

bool Connection::Init(){
    QSqlQuery* query=CreateQueryConn();
    QString sql="create table config (guid int NOT NULL,"
    		"name varchar(32) NOT NULL,"
    		"value ntext NOT NULL,"
    		"PRIMARY KEY(guid,name))";
    bool b=true;
    b=b&query->exec(sql);
    sql="create table article (guid int PRIMARY KEY,"
    		"title ntext NOT NULL,"
    		"content ntext NOT NULL,"
    		"createtime int NOT NULL,"
    		"valid smallint)";
	b=b&query->exec(sql);
    sql="create table multimedia (guid int PRIMARY KEY,"
    		"articleguid int NOT NULL,"
    		"labelid int NOT NULL,"
    		"type smallint NOT NULL,"
    		"path ntext NOT NULL)";
	b=b&query->exec(sql);
	if(b){
		sql="insert into config(name,value) values('createdate','"+QTime::currentTime().toString()+"')";
		b=b&query->exec(sql);
	}
	return b;
}
